package com.ybear.wechatpatdemo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.ybear.patsdk.widget.PatView;
import com.ybear.wechatpatdemo.R;
import com.ybear.wechatpatdemo.holder.PatMsgHolder;
import com.ybear.wechatpatdemo.holder.PatNotifyHolder;
import com.ybear.ybcomponent.base.adapter.BaseRecyclerViewAdapter;
import com.ybear.ybcomponent.base.adapter.BaseViewHolder;
import com.ybear.ybcomponent.base.adapter.IItemData;

import java.util.List;

/**
 * 拍一拍列表适配器
 */
public class PatAdapter extends BaseRecyclerViewAdapter<PatAdapter.ItemData, BaseViewHolder> {

    public PatAdapter(@NonNull List<ItemData> list) {
        super(list);
        //禁用点击样式
        setEnableTouchStyle( false );
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //根据类型判断当前数据是消息还是拍一拍通知
        return viewType == MsgType.MSG ?
                new PatMsgHolder( parent, R.layout.item_pat_msg ) :
                new PatNotifyHolder( parent, R.layout.item_pat_notify );
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder h, int position) {
        super.onBindViewHolder( h, position );
        ItemData data = getItemData( position );
        int type = getItemViewType( position );
        if( data == null ) return;

        switch ( type ) {
            case MsgType.MSG:       //消息
                PatMsgHolder msgHolder = ((PatMsgHolder)h);
                //消息
                msgHolder.getMsg().setText( data.getData() );
                //拍一拍点击事件监听器
                msgHolder.getPatView().setOnPatClickListener(v -> {
                    //显示拍一拍
                    showPatNotify( msgHolder.getContext() );
                    if( mOnPatClickListener != null ) mOnPatClickListener.onClick( v );
                    return true;
                });
                break;
            case MsgType.NOTIFY:    //通知
                ((PatNotifyHolder)h).getNotify().setText( data.getData() );
                break;
        }
    }

    /**
     * 根据类型判断消息的类型
     * @param position      当前下标
     * @return              类型
     */
    @Override
    public int getItemViewType(int position) {
        ItemData data = getItemData( position );
        return data == null ? MsgType.MSG : data.getMsgType();
    }

    private View.OnClickListener mOnPatClickListener;
    public void setOnPatClickListener(View.OnClickListener l) {
        mOnPatClickListener = l;
    }

    private void showPatNotify(Context context) {
        ItemData notifyData = new ItemData();
        notifyData.setData(String.format(
                context.getResources().getString( R.string.stringPatNotify ),
                "你",
                "自己"
        ));
        notifyData.setMsgType( MsgType.NOTIFY );
        addItemData( notifyData);
        notifyItemInserted( getItemCount() - 1 );
    }

    /**
     * 数据实体
     */
    public static class ItemData implements IItemData {
        @MsgType
        private int msgType = MsgType.MSG;
        private String data;

        int getMsgType() { return msgType; }
        public void setMsgType(int msgType) { this.msgType = msgType; }

        String getData() { return data; }
        public void setData(String data) { this.data = data; }
    }

    /**
     * 消息类型
     */
    public @interface MsgType {
        int MSG = 0;        //拍一拍
        int NOTIFY = 1;     //通知
    }
}
